package com.theme.passport.controller.theme;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.theme.common.mvc.utils.PageUtils;
import com.theme.common.mvc.view.R;
import com.theme.passport.dao.SysUserDao;
import com.theme.passport.entity.SysUserTestEntity;
import com.theme.passport.entity.ThirdUserEntity;
import com.theme.passport.entity.UserEntity;
import com.theme.passport.entity.theme.WidgeConfigEntity;
import com.theme.passport.service.ThirdUserService;
import com.theme.passport.service.UserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;


/**
 * 主题用户
 *
 * @author live
 * @email 870459550@qq.com
 * @date 2020-11-23 10:57:59
 */
@RestController
@RequestMapping("theme/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private ThirdUserService thirdUserService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("theme:user:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = userService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("theme:user:info")
    public R info(@PathVariable("id") Long id) {
        UserEntity user = userService.getById(id);

        return R.ok().put("user", user);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("theme:user:save")
    public R save(@RequestBody UserEntity user) {
        user.setCreateTime(new Date());
        userService.save(user);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("theme:user:update")
    public R update(@RequestBody UserEntity user) {
        userService.updateUserById(user);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("theme:user:delete")
    public R delete(@RequestBody Long[] ids) {
        userService.removeByIds(Arrays.asList(ids));
        //删除关联表信息：
        List<Long> userIds = Arrays.asList(ids);
        userIds.forEach(userId -> {
            ThirdUserEntity thirdUser = thirdUserService.queryByUserId(userId);
            if (thirdUser != null) {
                thirdUserService.removeById(thirdUser.getId());
            }
        });
        return R.ok();
    }




}
